
  //set导航条颜色toApp
  var barStyleParams = {};

  //点击弹框的 确定 按钮，弹框消失
    $('.weui_btn_dialog.primary').on('click',function() {
        $(this).parents('.weui_dialog_alert').hide();
    });

  barStyleParams.barFontColor = "0";//0:字体颜色为黑色     1：字体颜色为白色
  util.setBarStyle(barStyleParams);

  var loginValidate = {
    getCodeFlag : true,
    key: 'innjia2016111703',
    init: function() {
      //this.isWeiXinNavigator();
      this.bindEvent();
    },
    bindEvent: function(){
      this.getPlatform();
      console.log( this.platform );
      // this.checkUserName();
      this.clickLoginBtn();
      this.clickGetCode();
    },
    clickGetCode: function() {
      this.getCodeBtn = $('.get-code');
      var _this = this;
        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; 
      _this.getCodeBtn.on('click',function() {
          if( !$('.phoneNum').val() ) {
            $(this).parents('.base-info').find('.weui_dialog_bd.prompt_text').html('请输入手机号');
            $(this).parents('.base-info').find('.weui_dialog_alert').show();
            return false;
        }
         
        if(!myreg.test($(".phoneNum").val())){ 
            $(this).parents('.base-info').find('.weui_dialog_bd.prompt_text').html('请输入正确手机号');
            $(this).parents('.base-info').find('.weui_dialog_alert').show();
            return false;
        } 
        if( _this.getCodeFlag ) {
          _this.getCodeFlag = false;
          _this.remainingTime();
          _this.getCode();
        }
      });
    },
    remainingTime: function() {
      var _this = this;
      var counting = 60;
      var timer = setInterval(function() {
        _this.getCodeBtn.html("重新获取(" + (counting--) + "S)");
        if( counting < 0 ) {
          _this.getCodeBtn.html("获取验证码");
          _this.getCodeFlag = true;
          clearInterval( timer );
        }
      },1000);
    },
    getCode: function() {
      this.rest({
            methodId: "4003",
            data: {'Phone': $(".phoneNum").val() },
            success: function(res) {
               // console.log("getCode",res);
            }
        });
    },
    clickLoginBtn: function() {
      var _this = this;
      // var phone = $('.phoneNum').value();
      var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; 
      $('.login-btn').on('click',function(){
       
         if( !$('.phoneNum').val() ) {
            $(this).parents('.base-info').find('.weui_dialog_bd.prompt_text').html('请输入手机号');
            $(this).parents('.base-info').find('.weui_dialog_alert').show();
            return false;
        }
         
        if(!myreg.test($(".phoneNum").val())){ 
            $(this).parents('.base-info').find('.weui_dialog_bd.prompt_text').html('请输入正确手机号');
            $(this).parents('.base-info').find('.weui_dialog_alert').show();
            return false;
        } 

        if( $('.phoneCodeNum').val() == '' ) {
            $(this).parents('.base-info').find('.weui_dialog_bd.prompt_text').html('请输入手机验证码');
            $(this).parents('.base-info').find('.weui_dialog_alert').show();
            return false;
        }
       
          _this.registerProcess();
        
      })
    },
    registerProcess: function() {
      var _this = this;
       _this.rest({
          methodId: 4001,
          data: {
              "phone": $('.phoneNum').val(),
              "Code": $('.phoneCodeNum').val(),
              "Password":$('.phoneNum').val().substring(5,12),
              "ConfirmPassword":$('.phoneNum').val().substring(5,12),
              "nickName": $('.phoneNum').val(),
              "platform": _this.platform
          },
          success: function (res) {
              if (res) {
                //console.log( "register success!",res )
                if ("0" === res.code) {

                } else {
                    var tip = res.res.msg;
                      $('.base-info').find('.weui_dialog_bd.prompt_text').html(tip);
                     $('.base-info').find('.weui_dialog_alert').show();   
                }
              }
          }
        })
    },
    getPlatform: function() {
      var ua = window.navigator.userAgent.toLowerCase();
      if( ua.match(/Android/i) == 'android' || ua.match(/Adr/i) == 'adr' ) {
        this.platform == 'android';
        return false;
      }
      if( !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) ) {
        this.platform == 'ios';
        return false;
      }
      this.platform = 'h5';
    },
    isWeiXinNavigator: function(){
      var ua = window.navigator.userAgent.toLowerCase();
      if(ua.match(/MicroMessenger/i) == 'micromessenger') {
        this.isWeiXinNavigatorFlag = true;
        return false;
      }else{
        this.isWeiXinNavigatorFlag = false;
        //alert('请使用微信浏览器打开！')
        return false;
      }
    },
    rest: function(opt){
      var _this = this;
      // if (opt.log) {
      //   console.log("==== signData ==== rest.post ====");
      //   console.log(_this.signature.run(opt.methodId, opt.data));
      // }
      if(!opt.methodId){
        throw new Error("methodId 不能为空！")
      }
      if(!opt.data){
        opt.data = {}
      }
      $.ajax({
        // url: "https://testuip.innjia.com/test/api/innjiauip",
        url: global.uip.url,
        // data: _this.signature(opt.methodId, opt.data),
        data: postdata(opt.methodId, opt.data),
        type: "post",
        success: opt.success,
        error: opt.error,
        complete: opt.complete
      })
    },
    orderObj: function(obj) {
      let o = {};
      Object.keys(obj).forEach((key) => {
        o[key] = obj[key];
      });
      return o;
    },
    param: function(obj) {
      let arr = [];
      for (let i in obj) {
        arr.push(i + '=' + obj[i]);
      }
      return arr.join('&');
    }
  };
  loginValidate.init();
